package com.qltv.utils;

import org.apache.log4j.Logger;

import com.qltv.pojo.SNSUserInfo;
import com.qltv.pojo.WeixinOauth2Token;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AdvancedUtil {
	private static final Logger logger = Logger.getLogger(AdvancedUtil.class);

	/**
	 * 获取网页授权凭证
	 * @param appId 公众账号的唯一标识
	 * @param appSecret 公众账号的密钥
	 * @param code
	 * @return WeixinAouth2Token
	 */
	public static WeixinOauth2Token getOauth2AccessToken(String appId,String appSecret,String code){
		WeixinOauth2Token wat = null;
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
		requestUrl.replace("APPID", appId);
		requestUrl.replace("SECRET", appSecret);
		requestUrl.replace("CODE", code);
		//获取网页授权凭证
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		if(null!=jsonObject){
			try {
				wat = new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getInt("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
			} catch (Exception e) {
				wat = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				logger.error("获取网页授权凭证失败，errcode:"+errorCode + ",errmsg:"+errorMsg);
			}
			
		}
		return wat;
	}
	
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static SNSUserInfo getSNSUserInfo(String accessToken,String openId){
		SNSUserInfo snsUserInfo = null;
		String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
		requestUrl.replace("ACCESS_TOKEN", accessToken);
		requestUrl.replace("OPENID", openId);
		//通过网页授权获取用户信息
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		if(null!=jsonObject){
			try {
				snsUserInfo = new SNSUserInfo();
				//用户唯一标识
				snsUserInfo.setOpenId(jsonObject.getString("openid"));
				//昵称
				snsUserInfo.setNickname(jsonObject.getString("nickname"));
				//性别
				snsUserInfo.setSex(jsonObject.getInt("sex"));
				//国家
				snsUserInfo.setCountry(jsonObject.getString("country"));
				//省份 
				snsUserInfo.setProvince(jsonObject.getString("province"));
				//城市
				snsUserInfo.setCity(jsonObject.getString("city"));
				//头像
				snsUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				//用户特权信息
				snsUserInfo.setPrivilegeList(JSONArray.toList(jsonObject.getJSONArray("privilege"),List.class));
			} catch (Exception e) {
				snsUserInfo = null;
				int errorCode = jsonObject.getInt("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				logger.error("获取用户信息失败，errcode:"+errorCode + ",errmsg:"+errorMsg);
			}
			
			
		}
		return snsUserInfo;
	}
}
